from sys import prefix
import networkx as nx

def gen_airtel_topo():
    g = nx.Graph()
    for line in open('envs/airtel/airtel1-only-inserts.csv'):
        arr = line.split(',')
        src = arr[1].split('-')[0]
        dst = arr[2].split('-')[0]
        g.add_edge(src, dst)

    print(len(g.nodes))
    print(len(g.edges))

    with open('envs/airtel/topo.txt', 'w') as f:
        for e in g.edges:
            f.write('%s %s %s %s\n' % (e[0], e[1], e[1], e[0]))

def gen_airtel_fib():
    for line in open('envs/airtel/airtel1-only-inserts.csv'):
        # +92.104.0.0/14,s12-1,s9-3,170
        arr = line.split(',')
        src = arr[1].split('-')[0]
        dst = arr[2].split('-')[0]
        ip, prefix = getIpPrefix(arr[0][1:])
        with open('envs/airtel/fib/%s' % src, 'a') as f:
            f.write('fw %d %d %s\n' % (ip, prefix, dst)) 

def getIpPrefix(str):
    ip, prefix = str.split('/')
    arr = ip.split('.')
    return (int(arr[0])<<24) + (int(arr[1])<<16)  + (int(arr[2])<<8) + int(arr[3]), int(prefix)

gen_airtel_fib()